<template>
  <div class=".list-pagination classification-container">
    <div class="classification-container-add">
      <el-button icon="el-icon-plus" type="success" size="small" @click="showSortDialog(true)">新增菜品分类</el-button>
      <el-button icon="el-icon-plus" type="primary" size="small" @click="showSortDialog(false)">新增套餐分类</el-button>
    </div>
    <el-table
      :data="list"
      border
      style="width: 100%"
      :loading="listLoading"
    >
      <el-table-column
        prop="name"
        label="分类名称"
        width="width"
      />
      <el-table-column
        prop="sortType"
        label="分类类型"
        align="center"
        :formatter="formatterType"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
        width="width"
      />
      <el-table-column
        prop="updateTime"
        label="操作时间"
        align="center"
        width="width"
      />
      <el-table-column
        prop="sort"
        label="排序"
        align="center"
        width="width"
      />
      <el-table-column label="操作" class="classification-container-table-button" width="200px" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleChange(scope.$index, scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="paginationInfo.total > 0"
      :total="paginationInfo.total"
      :page.sync="paginationInfo.page"
      :limit.sync="paginationInfo.pageSize"
      @pagination="fetchData"
    />
    <add-sort-dialog v-if="showDialog" :show.sync="showDialog" :is-dish="isDish" :is-edit="isEdit" :detail-data="detailData" />
  </div>
</template>

<script>
import { getCategoryList, deleteCategory } from '@/api/category'
import AddSortDialog from '@/views/classification/components/AddSortDialog'
import { EventBus } from '@/utils/eventBus'
import pagination from '@/components/pagination'

export default {
  components: { AddSortDialog, pagination },
  data() {
    return {
      list: [],
      listLoading: true,
      showDialog: false,
      isDish: false,
      isEdit: false,
      detailData: {},
      paginationInfo: {
        pageSize: 10,
        page: 1,
        total: 0
      }
    }
  },
  created() {
    this.paginationInfo.page = 1
    this.fetchData()
  },
  mounted() {
    EventBus.$on('retchCategoryData', () => {
      this.paginationInfo.page = 1
      this.fetchData()
    })
  },
  methods: {
    async fetchData(obj) {
      if (obj) {
        const { page, limit } = obj
        this.paginationInfo.page = page
        this.paginationInfo.pageSize = limit
      }
      this.listLoading = true
      const { page, pageSize } = this.paginationInfo
      const { data } = await getCategoryList({ page, pageSize })
      if (data) {
        this.list = data?.records || []
        this.paginationInfo.total = data?.total || 0
      }
      this.listLoading = false
    },
    handleChange(index, row) {
      console.log(row)
      this.detailData = row
      this.isEdit = true
      this.showDialog = true
    },
    async handleDelete(index, row) {
      this.$confirm('是否删除所选商品', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteCategory(row.id)
        if (res?.code) {
          this.$message.success('删除成功')
          this.paginationInfo.page = 1
          this.fetchData()
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    showSortDialog(value) {
      this.isDish = value
      this.showDialog = true
      this.isEdit = false
    },
    formatterType(row) {
      console.log(row)
      return row.type === 1 ? '菜品分类' : '套餐分类'
    }
  }
}
</script>

<style lang="scss">
  .classification-container{
    margin: 10px;
     &-add{
      margin: 20px;
      .el-button{
        &:hover{
          color: black;
        }
      }
    }

     &-table{
      &-button{
        border: 0;
      }
     }
  }
</style>
